package dynaspring.support;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.PropertyPathFactoryBean;
import org.springframework.util.StringUtils;

public abstract class Util {
	
    public static PropertyPathFactoryBean makePropertyPathFactoryBean(String path, BeanFactory beanFactory) {
        if (!StringUtils.hasText(path)) {
        	throw new RuntimeException("propertyPath must not be empty");
        }
        int dotIndex = path.indexOf(".");
        if (dotIndex == -1) {
        	throw new RuntimeException("propertyPath must follow pattern 'beanName.propertyName': " + path);
        }
        String beanName = path.substring(0, dotIndex);
        String propertyPath = path.substring(dotIndex + 1);
        PropertyPathFactoryBean p = new PropertyPathFactoryBean();
        p.setTargetBeanName(beanName);
        p.setPropertyPath(propertyPath);
        p.setBeanFactory(beanFactory);
        return p;
    }
    
    /**
     * Used as a dummy factory-method for import-bean. Returns its argument.
     * @param obj
     * @return obj
     */
    public static Object importBean(Object obj) {
    	return obj;
    }

}
